package cn.usbtg.session.impl;

import cn.usbtg.session.BTGSession;
import cn.usbtg.session.BTGSessionDao;
import cn.usbtg.session.BTGSessionIdGenerator;
import cn.usbtg.session.BTGSessionManager;

/**
 * 自定义session管理器
 */
public class BTGStandardSessionManager implements BTGSessionManager {

    //session id 生成器
    private BTGSessionIdGenerator sessionIdGenerator;
    //session存储器
    private BTGSessionDao sessionDao;

    public BTGStandardSessionManager() {
        this.sessionIdGenerator = new BTGUUIDSessionIdGenerator();
        this.sessionDao = new BTGLocalSessionDao();
    }

    public BTGStandardSessionManager(BTGSessionDao sessionDao) {
        this.sessionIdGenerator = new BTGUUIDSessionIdGenerator();
        this.sessionDao = sessionDao;
    }

    public BTGStandardSessionManager(BTGSessionIdGenerator sessionIdGenerator) {
        this.sessionIdGenerator = sessionIdGenerator;
        this.sessionDao = new BTGLocalSessionDao();
    }

    public BTGStandardSessionManager(BTGSessionIdGenerator sessionIdGenerator, BTGSessionDao sessionDao) {
        this.sessionIdGenerator = sessionIdGenerator;
        this.sessionDao = sessionDao;
    }

    public BTGStandardSessionManager(BTGSessionDao sessionDao, BTGSessionIdGenerator sessionIdGenerator) {
        this(sessionIdGenerator, sessionDao);
    }

    @Override
    public BTGSession getSession(String sessionId) {
        if (null == sessionId || sessionId.length() == 0) return null;
        return this.sessionDao.getSession(sessionId);
    }

    @Override
    public BTGSession getNewSession() {
        return new BTGStandardSession(this.sessionIdGenerator.genaeratorSessionId());
    }

    @Override
    public void addSession(BTGSession session) {
        if (null == session) return;
        this.sessionDao.saveSession(session);
    }

    @Override
    public void removeSession(String sessionId) {
        if (null == sessionId || sessionId.length() == 0) return;
        this.sessionDao.deleteSession(sessionId);
    }

}